**************************************
*generated: 2024-07-25
*
*by Lukas Rudolph, Franziska Quoss
*
*Data used:  w4_final.csv
*Data output generated: w4_final_reshaped.dta
*Tables generated: none
*Figutes generated: none
**************************************


*specify version
version 17

*set working directory to path containing replication data
*cd ""

*load data
insheet using "w4_final.csv", clear

 
*********************************
*recoding missings

*code missings for string variables and destring
foreach var of varlist _all {
capture replace `var' = "." if `var' == "NA" | `var' == " NA"
capture destring `var', replace
}


*********************************
*reshaping dataset

*rename variables for reshaping
forvalues l = 1/18 {
foreach x in  _id _listplace _firstname _lastname _age _location _partyid _partyname _partyenv _envscore _leftright _rur _sv_diff_abs _lr_diff_abs _pid_align _gender _localitymatch {
rename w4_cand`l'`x' w4`x'_cand`l'
}
}

*reshape to long format
reshape long w4_q33x w4_gender_cand w4_id_cand w4_listplace_cand w4_firstname_cand w4_lastname_cand w4_age_cand w4_location_cand w4_partyid_cand w4_partyname_cand w4_partyenv_cand w4_envscore_cand w4_leftright_cand w4_rur_cand w4_pid_align_cand w4_localitymatch_cand w4_sv_diff_abs_cand w4_lr_diff_abs_cand, i(pubid) j(choice)


*********************************
*recoding 

*****
*difference of respondent environmental/left-right score to candidates environmental/left-right score 

gen env_score_diff = abs(env_score_ego - w4_envscore_cand)

gen lr_diff = abs(w4_q22_rescaled - w4_leftright_cand)


****
*gen party mean and difference respondent to party mean and candidate deviation from party mean

egen partymean_env = mean(w4_envscore_cand), by(pubid w4_partyid_cand)
egen partymean_lr = mean(w4_leftright_cand), by(pubid w4_partyid_cand)

gen partymean_env_diff = abs(env_score_ego- partymean_env)
gen partymean_lr_diff = abs(w4_q22_rescaled-partymean_lr)

gen canddiff_partydiff_env = env_score_diff - partymean_env_diff 
gen canddiff_partydiff_lr = lr_diff - partymean_lr_diff 


*****
*indicator for respondent party ID matching candidate on ballot

/* overview on value labels of party ID and candidates on ballot
w4_q23 
Value Labels
1 Schweizerische Volkspartei (SVP)
2 Sozialdemokratische Partei (SP)
3 Freisinnig-Demokratische Partei (FDP)
4 Christlichdemokratische Volkspartei (CVP)
5 Bürgerlich-Demokratische Partei (BDP)
6 Grüne Partei (GPS)
7 Grünliberale Partei (GLP)
8 Evangelische Volkspartei (EVP)
9 Eine andere Partei, nämlich:

w4_partyid_cand 
		 1  CVP  
         2 	FDP  
         3  SVP  
         4  SP  
         5  GP  
         6  glp  
         7  BDP  
         8  CSP  
		 9 Lega
		 10  PdA  
*/

recode w4_partyid_cand (1 = 4 "CVP") (2 = 3 "FDP") (3=1 "SVP") (4=2 "SP") (5=6 "GP") (6=7 "glp") (7=5 "BDP") (8=10 "CSP") (10=11 "PdA") (9 = 12 "Lega"), gen(w4_partyid_candv2)

gen partyidmatch = w4_q23 == w4_partyid_candv2


****
*generate indicator for candidate gender matching respondents gender
gen gendermatch = w4_q2 == w4_gender_cand

****
*generate indicator for candidate age matching respondents age within 5 or 5-10 years
gen age = 2020-w4_q1
gen agematch = abs(age-w4_age_cand)
gen agematch2 = 0 
replace agematch2 = 1 if agematch <= 5
replace agematch2 = 2 if agematch > 5 & agematch <= 10


****
*generate indicator for candidate home rurality classification matching respondents home rurality classification 
gen ruralmatch = 0 
foreach x in 1 2 3 {
	foreach y in 1 2 3 {
replace ruralmatch = 1 if w4_rur_cand == `x' & rurality == `y'
}
}
foreach x in 4 5 6  {
	foreach y in 3 4 5 6  {
replace ruralmatch = 1 if w4_rur_cand == `x' & rurality == `y'
}
}
foreach x in 7 8 9  {
	foreach y in 7 8 9  {
replace ruralmatch = 1 if w4_rur_cand == `x' & rurality == `y'
}
}


***
*generate detailed matching variables 

rename w4_localitymatch_cand localitymatch

gen femalegendermatch = gendermatch if w4_q2 == 1
gen malegendermatch = gendermatch if w4_q2 == 2
replace femalegendermatch = 0 if femalegendermatch == . & gendermatch != .
replace malegendermatch = 0 if malegendermatch == . & gendermatch != .

gen youngage = 0
replace youngage = 1 if age <= 35
gen youngagematch = agematch if youngage == 1
gen oldagematch = agematch if youngage == 0
replace youngagematch = 0 if youngagematch == . & agematch != .
replace oldagematch = 0 if oldagematch == . & agematch != .

gen youngagematch2 = agematch2 if youngage == 1
gen oldagematch2 = agematch2 if youngage == 0
replace youngagematch2 = 0 if youngagematch2 == . & agematch2 != .
replace oldagematch2 = 0 if oldagematch2 == . & agematch2 != .

label define agematch2 0 "Agediff. 10+ yrs" 1 "Agediff. less 5 yrs" 2 "Agediff. 5 to 10 yrs", replace
label values *agematch2 agematch2

gen urbanlocalitymatch = localitymatch if rurality <= 2
gen periurbanlocalitymatch = localitymatch if rurality > 2 & rurality <= 6
gen rurallocalitymatch = localitymatch if rurality >= 7 & rurality <= 9
replace urbanlocalitymatch = 0 if urbanlocalitymatch == . & localitymatch != .
replace periurbanlocalitymatch = 0 if periurbanlocalitymatch == . & localitymatch != .
replace rurallocalitymatch = 0 if rurallocalitymatch == . & localitymatch != .


****
*generate listplace indicators from listplaces as shown on ballot
recode w4_listplace_cand (1 = 1 "1") (2 = 2 "2") (3/4 = 3 "3/4") (5/7 = 4 "5/7") (8/10 = 5 "8/10") (11/20 = 6 "11/20") (21/35 = 7 "21/35"), gen(w4_listplace_cand_cat)


****
*generate indicator vor candidate displayed in first, second or third position within party on ballot
gen displayplace = .
foreach x in  1 4 7 10 13 16 {
	replace displayplace = 1 if choice == `x'
		replace displayplace = 2 if choice == `x'+1
		replace displayplace = 3 if choice == `x'+2

}

****
*set labels for values of treatment group indicator
label define w4_treat6 1 "control" 2 "env" 3 "lr" 4 "lr & env", replace
label values w4_treat6 w4_treat6 

* change label for treatment indicator so that it looks good in latex
gen w4_treat6_cor = w4_treat6
label define treat 1 "control" 2 "env" 3 "lr" 4 "lr \& env", replace
label values w4_treat6_cor treat 

****
*set labels for variables

label variable w4_q33x "Vote for cand. (cont.)"

label variable w4_treat6 "Treatment"

label variable lr_diff "Diff. LR to cand."
label variable env_score_diff "Diff env. to cand."
label variable canddiff_partydiff_env "Cand.-Party-Ego-Dist._Env"
label variable canddiff_partydiff_lr "Cand.-Party-Ego-Dist._LR"
label variable partymean_lr_diff "Diff LR to party"
label variable partymean_env_diff "Diff env to party"

label variable partyidmatch "PID match"
label variable ruralmatch "Rural/urban match"
label variable gendermatch "Gendermatch"
label variable agematch2 "Agematch"
label variable localitymatch "Municipality match"

label variable urbanlocalitymatch "Municipality match (urban)"
label variable periurbanlocalitymatch "Municipality match (periurban)"
label variable rurallocalitymatch "Municipality match (rural)"

label variable malegendermatch "Gendermatch (male)"
label variable femalegendermatch "Gendermatch (female)"

label variable displayplace "Displayorder"
label variable w4_listplace_cand_cat "Listplace"


*cmset data
cmset pubid choice

***********
*saving
save w4_final_reshaped.dta, replace










